<template>
    <div>
        <Form :label-width="100" ref="form1" :model="form1">
            <Row  v-show="!switchFlag" class="query-form2">
                <Col span="8">
                    <FormItem label="填报日期：" prop="reportDate">
                        <DatePicker type="date" style="width: 100%"  v-model="form1.reportDate"  format="yyyy-MM-dd"  placeholder="请选择" ></DatePicker>
                    </FormItem>
                </Col>
                <Col span="8">
                    <FormItem label="所属单位：" prop="orgId" >
                        <cascaderDom style="width: 100%" ref="childConOrg"  @setOrgInfo="setQueryOrgId"  @setOrgname="setQueryOrgName" :currentOrgId = "currentOrgId"
                                     :currentOrgName = "currentOrgName"   :orgTree = "true"  ></cascaderDom>
                    </FormItem>
                </Col>
                <Col span="8">
                    <div style="float: right;">
                        <Button class="ivu-btn-query"  icon="md-search"  @click="query">查询</Button>
                        <Button class="ivu-btn-reset" style="margin-left: 10px" @click="reset">重置</Button>
                        <Button @click="switchQuery(true)" style="margin-left: 5px" class="ivu-btn-switch2" type="text" >展开<Icon type="ios-arrow-down"/></Button>
                    </div>
                </Col>
            </Row>
            <Row  v-show="switchFlag" class="query-form2">
                <Col span="8">
                    <FormItem label="填报日期：" prop="reportDate">
                        <DatePicker type="date"  style="width: 100%" v-model="form1.reportDate" class="query-form-el"  format="yyyy-MM-dd"  placeholder="请选择" ></DatePicker>
                    </FormItem>
                </Col>
                <Col span="8">
                    <FormItem label="所属单位：" prop="orgId" >
                        <cascaderDom class="query-form-el" style="width: 100%" ref="childConOrg"  @setOrgInfo="setQueryOrgId"  @setOrgname="setQueryOrgName" :currentOrgId = "currentOrgId"
                                     :currentOrgName = "currentOrgName"   :orgTree = "true"  ></cascaderDom>
                    </FormItem>
                </Col>
                <Col span="8">
                    <FormItem label="作业类型：" prop="workType">
                        <Select class="query-form-el" filterable style="width: 100%" v-model="form1.workType"  >
                            <Option v-for="item in ptwTypeList" :value="item.codingCodeId" :key="item.codingCodeId">{{ item.codingName }}</Option>
                        </Select>
                    </FormItem>
                </Col>
            </Row>
            <Row  v-show="switchFlag" class="query-form2">
                <Col span="8">
                    <FormItem label="作业等级：">
                        <Select  filterable v-model="form1.workGrade" style="width: 100%" >
                            <Option v-for="item in workGradeList" :value="item.codingCodeId" :key="item.codingCodeId">{{ item.codingName }}</Option>
                        </Select>
                    </FormItem>
                </Col>
                <Col span="8">
                    <FormItem label="填写人：">
                        <cascaderDom style="width: 100%" @setOrgInfo="setQueryUserId" @setOrgname="setQueryUserName"    ></cascaderDom>
                    </FormItem>
                </Col>
                <Col span="8">
                    <FormItem label="审核状态：">
                        <Select  filterable v-model="form1.workType" style="width: 100%" >
                        </Select>
                    </FormItem>
                </Col>
            </Row>
            <Row v-show="switchFlag" class="query-form2">
                <div style="float: right;height:44px">
                    <Button class="ivu-btn-query"  icon="md-search"  @click="query">查询</Button>
                    <Button class="ivu-btn-reset" style="margin-left: 10px" @click="reset">重置</Button>
                    <Button @click="switchQuery(false)" style="margin-left: 10px" class="ivu-btn-switch2" type="text" >收起<Icon type="ios-arrow-up" /></Button>
                </div>
            </Row>
            <Row class="query-form">
                <Col span="24">
                    <Button size="large" type="success" class="ivu-btn-add" icon="md-add"  @click="addInfo">新增</Button>
                    <!--<Button size="large" class="ivu-btn-delete" icon="md-trash"  @click="batDelete">删除</Button>-->
                </Col>
            </Row>
        </Form>

        <Table ref='table' :height="tableHeight"  :loading="loading" @on-selection-change = 'getSelectRows'
                border :columns="columns1"  :data="dataTable">
        </Table>

        <Row style="margin-right:10px;">
            <Col span="24" >
                <Page style="float: right" :total="pagedata.total" :page-size="pagedata.pageSize" @on-change="pageChange" @on-page-size-change="pageSizeChange" :page-size-opts="pageSizeArray" show-total show-sizer show-elevator />
            </Col>
        </Row>

        <Modal title="作业方案新增" :transfer=false v-model="model1"  :width=600>
            <Row>
                <Col span="24">
                    <Form ref="form1datacopy" :model="form1datacopy" :rules="ruleCustom" label-position="top" >
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="11" >
                                <FormItem label="方案名称：" prop="docName">
                                    <Input type="text" v-model="form1datacopy.docName" ></Input>
                                </FormItem>
                            </Col>
                            <Col span="11">
                                <FormItem label="所属单位：" prop="operateOrg" >
                                    <cascaderDom ref="childOrg"  @setOrgInfo="setOrgId"  @setOrgname="setOrgName" :currentOrgId = "currentOrgId"
                                                 :currentOrgName = "currentOrgName"   :orgTree = "true"  ></cascaderDom>
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="11" >
                                <FormItem label="作业类型：" prop="workType">
                                    <Select filterable v-model="form1datacopy.workType"  >
                                        <Option v-for="item in ptwTypeList" :value="item.codingCodeId" :key="item.codingCodeId">{{ item.codingName }}</Option>
                                    </Select>
                                </FormItem>
                            </Col>
                            <Col span="11">
                                <FormItem label="等级：" prop="workGrade" >
                                    <Select filterable v-model="form1datacopy.workGrade"  >
                                        <Option v-for="item in workGradeList" :value="item.codingCodeId" :key="item.codingCodeId">{{ item.codingName }}</Option>
                                    </Select>
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="11">
                                <FormItem label="创建人：" prop="reportUserName">
                                    <Input v-model="form1datacopy.reportUserName" readonly></Input>
                                </FormItem>
                            </Col>
                            <Col span="11" >
                                <FormItem label="填报日期：" prop="reportDate">
                                    <DatePicker type="date"  format="yyyy-MM-dd"   v-model="form1datacopy.reportDate"  style="width: 100%"  readonly placeholder="请选择" ></DatePicker>
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="11">
                                <FormItem label="审批人：" prop="approveUserName">
                                    <Select
                                        placeholder="请输入关键字查询"
                                        v-model="form1datacopy.approveUserId"
                                        filterable
                                        :remote-method="remoteMethod1"
                                        :loading="loading1">
                                        <Option v-for="(option, index) in options1" :value="option.userId" :label="option.userName" :key="index">
                                            <span>{{option.userName}}</span>
                                            <span style="float:right;color:#ccc">{{option.orgInfo.orgName}}</span>
                                        </Option>
                                    </Select>
                                </FormItem>
                            </Col>
                            <Col span="11"></Col>
                        </Row>
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="23">
                                <FormItem label="上传附件：">
                                    <Upload
                                            ref="upload"
                                            type="drag"
                                            :show-upload-list="true"
                                            :before-upload="beforeUpload"
                                            :action="upUrl"
                                            :max-size="10240"
                                        >
                                        <div style="padding: 20px 0">
                                            <Icon type="ios-cloud-upload" size="52" style="color: #3399ff"></Icon>
                                            <p>点击或拖拽文件到这上传</p>
                                        </div>
                                    </Upload>
                                    <div v-if="fileName !== null">
                                        附件名称: {{ fileName }}
                                        <Button type="text" icon="md-trash" @click="deleteCover(docFile)"
                                                  style="margin-left: 20px;margin-bottom: 3px"/>
                                    </div>
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="23">
                                <FormItem label="备注：" prop="notes">
                                    <Input v-model="form1datacopy.notes" type="textarea" :rows="2" />
                                </FormItem>
                            </Col>
                        </Row>
                    </Form>
                </Col>
            </Row>
            <div slot="footer" >
                <Button @click="submit('form1datacopy')" class="ivu-btn-save" :loading="buttonLoading">确定</Button>
                <Button @click="handleCancel" class="ivu-btn-cancel" >取消</Button>
            </div>
        </Modal>

        <Modal title="作业计划" :transfer=false footer-hide v-model="planForSelectModal"  :width=900 >
            <PlanForSelect  @getSelectData="getSelectData"  >
            </PlanForSelect>
        </Modal>

        <Modal title="作业方案详情"  class="modal-view-detail"  :transfer=false v-model="model2"  :width=1100>
            <Row>
                <Col span="24">
                    <Form  :model="showdata"  :label-width="160" >
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="11" >
                                <FormItem label="方案名称：" prop="docName">
                                    {{showdata.docName}}
                                </FormItem>
                            </Col>
                            <Col span="11">
                                <FormItem label="所属单位：" prop="operateOrg" >
                                     {{showdata.operateOrg}}
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="11" >
                                <FormItem label="作业类型：" prop="workType">
                                    {{showdata.workTypeName}}
                                </FormItem>
                            </Col>
                            <Col span="11" >
                                <FormItem label="作业等级：" prop="workGrade" >
                                    {{showdata.workGradeName}}
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="11">
                                <FormItem label="创建人：" prop="reportUserName">
                                    {{showdata.reportUserName}}
                                </FormItem>
                            </Col>
                            <Col span="11" >
                                <FormItem label="填报日期：" prop="reportDate">
                                    {{showdata.reportDate}}
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="11">
                                <FormItem label="审核人：" prop="approveUserName">
                                    {{showdata.approveUserName}}
                                </FormItem>
                            </Col>
                            <Col span="11" >
                            </Col>
                        </Row>
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="11">
                                <FormItem label="附件：" prop="docFile">
                                    <a :href="downloadUrl">{{fileName}}</a>
                                </FormItem>
                            </Col>
                            <Col span="11"></Col>
                        </Row>
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="23">
                                <FormItem label="备注：" prop="notes">
                                    {{showdata.notes}}
                                </FormItem>
                            </Col>
                        </Row>
                    </Form>
                </Col>
            </Row>
            <div slot="footer" >
                <Button class='ivu-btn-save' @click="model2=false">关闭</Button>
            </div>
        </Modal>
	</div>
</template>

<script>
	import {Api} from '../../../../api/index.js'
    import cascaderDom from "../../../../components/cascader-dom.vue"
    import PlanForSelect from "./planForSelect";
    import { sessionStorage, cookie } from '../../../../common/cacheUtils.js';
	export default {
        components: {
            PlanForSelect,
            cascaderDom
        },
		data() {
			return {
                loading: false,
                loading1: false,
                options1: [],
                buttonLoading: false,
                switchFlag : false,
                planForSelectModal : false,
                upUrl:'',
				form1: {
				},
                //分页参数
                pageSizeArray: [10, 20, 50],
                pagedata: {
                    total: 0,
                    pageSize: 10,
                    currentPage: 1,
                },
				columns1: [
				    /*{
                        type: 'selection',
                        title: '序号',
						 width: 80,
						 align: 'center',
					},*/
                    {
                        title: '序号',
                        width: 100,
                        align: 'center',
                        render: (h, params) => {
                            return h('span', params.index + (this.pagedata.currentPage - 1) * this.pagedata.pageSize + 1);
                        }
                    },
					{
                        minWidth: 150,
                        title: '填报日期',
                        align: 'center',
                        key: 'reportDate',
                        render:(h,params)=>{
                            return h('div',[
                                (this.formatDate(new Date(params.row.reportDate), 'yyyy-MM-dd'))
                            ]);
                        }
					},
					{
                        minWidth: 150,
						title: '所属单位',
						key: 'operateOrg',
						align: 'center'
					},
                    {
                        minWidth: 210,
                        title: '方案名称',
                        key: 'docName',
                        align: 'center'
                    },
					{
					    minWidth: 210,
						title: '作业类型',
						key: 'workTypeName',
                        align: 'center'
					},
					{
					    minWidth: 210,
						title: '等级',
						key: 'workGradeName',
						align: 'center'
					},
                    {
                        minWidth: 210,
                        title: '创建人',
                        key: 'reportUserName',
                        align: 'center'
                    },
                    {
                        minWidth: 100,
                        title: '审核状态',
                        key: 'procinstStateName',
                        align: 'center'
                    },
                    {
                        title: '操作',
                        key: 'action',
                        fixed: 'right',
                        minWidth: 270,
                        align: 'center',
                        render: (h, params) => {
                            if (params.row.procinstState == '0' || params.row.procinstState == null) {
                                return h('div', [
                                    h('A', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        fontSize: '14px'
                                    },
                                    on: {
                                        click: () => {
                                            this.showData(params, 1);
                                        }
                                    }
                                    }, '详情'),
                                    h('Divider', {
                                        props: {
                                            type: 'vertical',
                                        }
                                    }, ''),
                                    h('A', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        fontSize: '14px',
                                        marginLeft: '8px'
                                    },
                                    on: {
                                        click: () => {
                                            this.showData(params, 2);
                                        }
                                    }
                                }, '修改'),
                                h('Divider', {
                                    props: {
                                        type: 'vertical',
                                    }
                                }, ''),
                                h('A', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        fontSize: '14px',
                                        marginLeft: '8px'
                                    },
                                    on: {
                                        click: () => {
                                            this.$Modal.confirm({
                                                title: '数据提交',
                                                content: '<p>是否要提交</p>',
                                                okText: '确认',
                                                onOk: () => {
                                                    this.report(params);
                                                },
                                                cancelText: '取消'
                                            });
                                        }
                                    }
                                }, "提交"),
                                h('Divider', {
                                    props: {
                                        type: 'vertical',
                                    }
                                }, ''),
                                h('A', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        fontSize: '14px',
                                        marginLeft: '8px',
                                        color: 'red'
                                    },
                                    on: {
                                        click: () => {
                                            this.$Modal.confirm({
                                                title: '数据删除',
                                                content: '<p>确定删除该条信息</p>',
                                                okText: '确认',
                                                onOk: () => {
                                                    this.deleteData(params);
                                                },
                                                cancelText: '取消'
                                            });
                                        }
                                    }
                                    }, '删除')
                                ]);
                            }else{
                                return h('div', [
                                    h('A', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        fontSize: '14px'
                                    },
                                    on: {
                                        click: () => {
                                            this.showData(params, 1);
                                        }
                                    }
                                    }, '详情'),
                                    h('Divider', {
                                        props: {
                                            type: 'vertical',
                                        }
                                    }, ''),
                                    h('A', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        fontSize: '14px',
                                        marginLeft: '8px',
                                        color: '#d0d0d0',
                                        pointerEvents: 'none'
                                    },
                                    on: {
                                        click: () => {
                                            this.showData(params, 2);
                                        }
                                    }
                                }, '修改'),
                                h('Divider', {
                                    props: {
                                        type: 'vertical',
                                    }
                                }, ''),
                                h('A', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        fontSize: '14px',
                                        marginLeft: '8px',
                                        color: '#d0d0d0',
                                        pointerEvents: 'none'
                                    },
                                    on: {
                                        click: () => {
                                            this.$Modal.confirm({
                                                title: '数据提交',
                                                content: '<p>是否要提交</p>',
                                                okText: '确认',
                                                onOk: () => {
                                                    this.report(params);
                                                },
                                                cancelText: '取消'
                                            });
                                        }
                                    }
                                }, "提交"),
                                h('Divider', {
                                    props: {
                                        type: 'vertical',
                                    }
                                }, ''),
                                h('A', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        fontSize: '14px',
                                        marginLeft: '8px',
                                        color: '#d0d0d0',
                                        pointerEvents: 'none'
                                    },
                                    on: {
                                        click: () => {
                                            this.$Modal.confirm({
                                                title: '数据删除',
                                                content: '<p>确定删除该条信息</p>',
                                                okText: '确认',
                                                onOk: () => {
                                                    this.deleteData(params);
                                                },
                                                cancelText: '取消'
                                            });
                                        }
                                    }
                                    }, '删除')
                                ]);
                            }
                        }
                    }
				],
                ruleCustom : {
                    operateOrg: [
                        { required: true, message: '  ', trigger: 'blur' }
                    ],
                    workType: [
                        { required: true, message: '  ', trigger: 'change' }
                    ],
                    workGrade: [
                        { required: true, message: '  ', trigger: 'change' }
                    ],
                    docName: [
                        { required: true, message: '  ', trigger: 'blur' }
                    ]
                },
                currentOrgId : '',
                currentOrgName: '',
                setCurrentName : {},
                setCurrentApproveName : {},
                form1datacopy : {
                    operateOrg: '',
                    workType: '',
                    workGrade: '',
                    docName : ''
                },
                dataTable: [],
                InputData: {},
                showdata: {},
                model1: false,
                model2: false,
                add: false,
                ptwTypeList:[],
                workGradeList : [],
                tableHeight : 450,
                deleteRows  : [],
                file : null,
                fileName : null,
                downloadUrl : Api.downloadPTWSchemeFile,
			}
		},
		mounted: function() {
            this.tableHeight = window.innerHeight - this.$refs.table.$el.offsetTop - 145;
            this.selectCodingSort();
			this.query();
        },
		methods: {
            getModelList(){
                let data = {
                    belongOrgId: sessionStorage('userId').getObject('userId').orgInfo.orgId,
                    userId:  sessionStorage('userId').getObject('userId').userId
                }
                this.$http.get(Api.queryPtwModelList, {params: data}).then((res) => {
                    this.formworks = res.data;
                }).catch(e => {
                    this.$Message.error("数据获取失败")
                })
            },
            switchQuery(flag){
                this.switchFlag = flag;
                if(this.switchFlag){
                    this.tableHeight = this.tableHeight - 88;
                }else{
                    this.tableHeight = this.tableHeight + 88;
                }
            },
            remoteMethod1(query){
                if (query !== '') {
                    clearTimeout(this.remoteMethod1.timer);
                    this.remoteMethod1.timer = setTimeout(()=>{
                        this.options1.splice(0,this.options1.length);
                        this.loading1 = true;
                        let data = {
                            userName: query,
                            orgId: sessionStorage('userId').getObject('userId').orgInfo.parentId
                        };
                        this.$http.get(Api.queryUserByUserName , {params: data}).then((res) => {
                            this.loading1 = false;
                            res.data.data.forEach(item=>{
                                this.options1.push(item);
                            })

                        }).catch(e => {
                            this.loading1 = false;
                        })
                    },200);
                } else {
                    this.options1 = [];
                }
            },
            report(row){

                //ptw-100004000000002  一级
                //ptw-100004000000003  二级
                //ptw-100004000000004  三级
                //ptw-100004000000001  特级
                let key = '';
                if(row.row.workGrade == 'ptw-100004000000001'||row.row.workGrade == 'ptw-100004000000002'){
                    key = 'classC';
                }else if(row.row.workGrade == 'ptw-100004000000003'){
                    key = 'classB';
                }else if(row.row.workGrade == 'ptw-100004000000004'){
                    key = 'classA';
                }  
                let user = sessionStorage('userId').get('userId');
                let userJson = JSON.parse(user);
                let data = {
                    workId : row.row.docId,
                    key : key,
                    createUserId : userJson.userId,
                    nextUserId : row.row.approveUserId,
                    formKey : 'schemeForApproval',
                    type: 'zyxk',
                    userName: userJson.userName
                };
                this.$http.post(Api.submit, data ).then((res) => {
                    this.query();
                })

            },
            getSelectData(row){
                this.form1datacopy.planId = row.planId;
                this.form1datacopy.planName = row.planName;
                this.planForSelectModal = false;
            },
            openPlanModel(){
                this.planForSelectModal = true;
            },
            beforeUpload(file) {
                this.file = file;
                this.fileName = file.name;
                return false;
            },
            selectCodingSort() {
                this.$http.get(Api.queryCodeDetailBySortId + '/ptw-100004', {params: {}}).then((res) => {
                    this.workGradeList = res.data.data;
                })
                this.$http.get(Api.queryCodeDetailBySortId + '/ptw-100001', {params: {}}).then((res) => {
                    this.ptwTypeList = res.data.data;
                })
            },

            setQueryUserName(data) {
                this.form1.reportUserName = data;
            },
            setQueryUserId(data) {
                this.form1.reportUserId = data[data.length - 1];
            },
            setQueryOrgId(data) {
                this.form1.orgId = data[data.length - 1];
            },
            setQueryOrgName(data) {
                this.form1.operateOrg = data;
            },
            setUserName(data) {
                this.form1datacopy.reportUserName = data;
            },
            setUserId(data) {
                this.form1datacopy.reportUserId = data[data.length - 1];
            },
            setApproveUserName(data) {
                this.form1datacopy.approveUserName = data;
            },
            setApproveUserId(data) {
                this.form1datacopy.approveUserId = data[data.length - 1];
            },
            setOrgId(data) {
                this.form1datacopy.orgId = data[data.length - 1];
            },
            setOrgName(data) {
                this.form1datacopy.operateOrg = data;
            },
            pageChange(pageNum) {
                this.pagedata.currentPage = pageNum;
                this.query();
            },
            pageSizeChange(pageSize) {
                this.pagedata.pageSize = pageSize;
                this.query();
            },

            copyArray(arr) {
                return JSON.parse(JSON.stringify(arr));
            },

            showData(data, flag) {
                this.currentOrgId  = data.row.orgId;
                this.currentOrgName = data.row.operateOrg;
                setTimeout(()=>{
                    this.$refs.childOrg.setCurrentOrg();
                },200)
                this.setCurrentName  = { value :  data.row.reportUserId, label :  data.row.reportUserName };
                this.setCurrentApproveName  = { value :  data.row.approveUserId, label :  data.row.approveUserName };

                //根据ID查询附件
                let url = Api.getPTWSchemeFile +'/' + data.row.docFile;
                this.$http.get(url, {params: {}}).then((res) => {
                    this.docFile = res.data.id;
                    this.fileName = res.data.fileName;
                    this.downloadUrl =  Api.downloadPTWSchemeFile + '/' + res.data.id;
                }).catch(e => {
                    //console.log(e)
                    this.$Message.error("数据获取失败")
                })

                if (flag == 1) {
                    //查看页面
                    this.showdata = data.row;
                    this.showdata.reportDate = this.formatDate(new Date(this.showdata.reportDate), 'yyyy-MM-dd');
                    this.model2 = true;
                } else {
                    //修改页面
                    this.form1datacopy = data.row;
                    this.options1.push({userId:data.row.approveUserId,userName:data.row.approveUserName,orgInfo:{orgName:"xxx"}});
                    this.model1 = true;
                }
            },
             deleteCover(id) {
                this.$http.delete(Api.uploadMongoFile + "/" + id).then((res) => {
                    if (res.data.result == "success") {
                        this.docFile = null;
                        this.fileName = null;
                    }
                }).catch(e => {
                    this.$Message.error("附件上传失败");
                });
            },
            getSelectRows (rows){
                this.deleteRows = rows;
            },
            batDelete(){
                let docId = '';
                this.deleteRows.forEach(item=>{
                    docId = docId  + item.docId + ";"
                });
                docId = docId.substring(0, docId.length - 1);

                let url = Api.deletePTWScheme;
                this.$http.get(url, {params: {docId : docId}}).then((res) => {
                    this.$Message.success('成功');
                    this.query()
                }).catch(e => {
                    this.$Message.error('请求失败' + e);
                })
            },
            deleteData(params) {
                let url = Api.deletePTWScheme;
                this.$http.get(url, {params: {docId :  params.row.docId}}).then((res) => {
                    this.$Message.success('成功');
                    this.query()
                }).catch(e => {
                    this.$Message.error('请求失败' + e);
                })
            },

            query() {
                this.loading = true;
                let data = {
                    currentPage: this.pagedata.currentPage,
                    size: this.pagedata.pageSize,
                    userId: sessionStorage('userId').getObject('userId').userId
                }

                if( this.form1.reportDate!='' && this.form1.reportDate!=undefined ){
                    this.form1.reportDate = this.formatDate(new Date(this.form1.reportDate), 'yyyy-MM-dd');
                }else{
                    delete this.form1['reportDate'];
                }
                data = Object.assign(data, JSON.parse(JSON.stringify(this.form1)));

                let url = Api.queryPTWScheme;
                this.$http.get(url, {params: data}).then((res) => {
                    this.dataTable = res.data.data;
                    this.loading = false;
                    this.dataTable.map(item=>{
                        if(item.procinstState =='0' || item.procinstState == null){
                            item.procinstStateName = '未上报';
                        }else if(item.procinstState =='1'){
                            item.procinstStateName = '已上报';
                        }else if(item.procinstState =='2'){
                            item.procinstStateName = '被退回';
                        }else if(item.procinstState =='3'){
                            item.procinstStateName = '已审批';
                        }
                    })
                    this.pagedata.total = res.data.totalSize;
                }).catch(e => {
                    this.$Message.error("数据获取失败")
                })
            },
            reset() {
                this.form1 = {}
            },
            handleCancel() {
                this.form1datacopy = {};
                this.file = null;
                this.fileName = null;
                this.model1 = false;
            },
            submit() {
                this.$refs['form1datacopy'].validate((valid) => {
                    if(valid) {
                        this.buttonLoading = true;
                        this.options1.forEach(item=>{
                            if(item.userId == this.form1datacopy.approveUserId)
                                this.form1datacopy.approveUserName = item.userName;
                        })
                        if (this.file!=null){
                            const file = this.file;
                            let formData = new FormData();
                            formData.append("file", file);
                            this.$http.put(Api.uploadMongoFile, formData).then((res) => {
                                if (res.data.result == "success") {
                                    this.form1datacopy.createUserId = sessionStorage('userId').getObject('userId').userId;
                                    this.form1datacopy.reportDate = this.formatDate(new Date(this.form1datacopy.reportDate), 'yyyy-MM-dd');
                                    this.form1datacopy.docFile = res.data.id;
                                    this.$http.post(Api.savePTWScheme, this.form1datacopy).then((res) => {
                                        this.$Message.success("保存成功")
                                        this.model1 = false;
                                        this.file = null;
                                        this.query();
                                        this.buttonLoading = false;
                                    }).catch(e => {
                                        this.buttonLoading = false;
                                        this.model1 = false;
                                        this.query();
                                        this.$Message.error("数据获取失败")
                                    })
                                } else {
                                    this.$Message.error("附件上传失败");
                                }
                            }).catch(e => {
                                this.buttonLoading = false;
                                this.model1 = false;
                                this.query();
                                this.$Message.error("数据获取失败")
                            })
                        }else{
                            this.form1datacopy.reportDate = this.formatDate(new Date(this.form1datacopy.reportDate), 'yyyy-MM-dd');
                            this.$http.post(Api.savePTWScheme, this.form1datacopy).then((res) => {
                                this.$Message.success("保存成功")
                                this.model1 = false;
                                this.buttonLoading = false;
                                this.file = null;
                                this.query();
                            }).catch(e => {
                                this.model1 = false;
                                this.query();
                                this.buttonLoading = false;
                                this.$Message.error("数据获取失败")
                            })
                        }
                    } else {
                        this.$Message.error('请填报完整!');
                    }
                })
            },
            addInfo() {
                this.currentOrgId = "";
                this.currentOrgName = "";
                this.form1datacopy = {};
                this.form1datacopy.workGrade = 'ptw-100004000000004',
                this.form1datacopy.reportUserId = sessionStorage('userId').getObject('userId').userId;
                this.form1datacopy.reportUserName = sessionStorage('userId').getObject('userId').userName;
                this.form1datacopy.reportDate = new Date();
                this.file = null;
                this.fileName = null;
                this.model1 = true;
            },
            formatDate(date, fmt) {
                if (/(y+)/.test(fmt)) {
                    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
                }
                let o = {
                    'M+': date.getMonth() + 1,
                    'd+': date.getDate(),
                    'h+': date.getHours(),
                    'm+': date.getMinutes(),
                    's+': date.getSeconds()
                };
                for (let k in o) {
                    if (new RegExp(`(${k})`).test(fmt)) {
                        let str = o[k] + '';
                        fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : this.padLeftZero(str));
                    }
                }
                return fmt;
            },
            padLeftZero(str) {
                return ('00' + str).substr(str.length);
            },
        }
	}
</script>
<style lang='css' scoped>
    @import '../../../../index.css';
</style>
